package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.*;
import com.woniu.smallmedical.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@CrossOrigin
@RestController
public class OutStorageMedicineController {

    @Autowired
    OutStoragemedicineMapper basicmessageapper;
    @Autowired
    OutStoragemessageMapper masicmessageapper;
    @Autowired
    BasicmessageMapper casicmessageapper;
    @Autowired
    StoragemedicineMapper dasicmessageapper;

    @Autowired
    InventoryMapper Iasicmessageapper;
    @Autowired
    InventoryDetailMapper Idasicmessageapper;

    //添加药品
    @GetMapping("/insertmedicineinserts")
    public String insertmedicine(Integer bmid, String number) {
        System.out.println("number:" + number);
        System.out.println("入库人员有id：" + bmid);
        //通过单号查外键
        String dan = number;
        System.out.println("单号" + dan);
        if (number.equals(null) || number == "") {
            return "";
        } else {
            OutStorageMessage storageMessage = masicmessageapper.selectDan(dan);
            Integer stid = storageMessage.getSid();
            System.out.println("入库的id是+" + storageMessage);
            //药品查出来的信息
            Basicmessage basicmessage = casicmessageapper.selectById(bmid);
            Integer count = 20;
            //计算总价
            Double allpurchase = count * basicmessage.getPurchase();
            Double allretail = count * basicmessage.getRetail();
            basicmessageapper.insertmessagess(null, number, basicmessage.getNickname(), basicmessage.getFactory(), count, basicmessage.getUnit(), basicmessage.getPurchase(), basicmessage.getRetail(), allpurchase, allretail, stid);

            List<Inventory> inventories = Iasicmessageapper.selectName();

            for(int i=0;i<inventories.size();i++){
            //同名的才能加如库
                //不同名字的只能加个数

           if(inventories.get(i).getName().equals(basicmessage.getNickname())){
               //查出来的总个数加上要加入的个数
               Integer counts = Iasicmessageapper.selectretail(basicmessage.getNickname());
               System.out.println("查出来的个数："+counts);
               Integer thiscounts=counts-count;
               Iasicmessageapper.updatecount(basicmessage.getNickname(),thiscounts);


               //查出来的总个数加上要加入的采购价
               Double purchaseamount = Iasicmessageapper.selectInventorypurchase(basicmessage.getNickname());
               System.out.println("查出来的采购价："+purchaseamount);
               Double thispurchaseamount=purchaseamount-allpurchase;
               Iasicmessageapper.updatepurchase(basicmessage.getNickname(),thispurchaseamount);


               //查出来的总个数加上要加入的销售价
               Double retailamount = Iasicmessageapper.selectInventoryretail(basicmessage.getNickname());
               Double thsretailamount=retailamount-allretail;
               Iasicmessageapper.updateretail(basicmessage.getNickname(),thsretailamount);
               System.out.println("查出来的销售价："+retailamount);

               Integer inid=Iasicmessageapper.selectbynickneame(basicmessage.getNickname());
               Idasicmessageapper.inserInventoryDetail(null, number,new Date(), "出库", count,thispurchaseamount,thsretailamount,inid);
//           }if(!inventories.get(i).getName().equals(basicmessage.getNickname())){
//               //这个是加入库存的药品 查的是 订单中的总价和总数 （前提 ：没有相同的药品）
//               Integer counts = basicmessageapper.selectAllcount(basicmessage.getNickname());
//               Double purchaseamount = basicmessageapper.selectAllpurchases(basicmessage.getNickname());
//               Double retailamount = basicmessageapper.selectAllretials(basicmessage.getNickname());
//  Iasicmessageapper.inserInventory(null,basicmessage.getNumbers(),basicmessage.getNickname(),basicmessage.getPerscription(),basicmessage.getMedicinespecs(),basicmessage.getFactory(),counts,purchaseamount,retailamount);
           }



            }





        }

        return "";
    }


    @GetMapping("/selectByIdmedicines")
    public List<OutStorageMedicine> selectById(Integer sid) {
        System.out.println("stid是：" + sid);
        if (sid == null) {
            return null;
        } else {
            List<OutStorageMedicine> storageMedicines = basicmessageapper.selectByIds(sid);
            System.out.println(storageMedicines);
            return storageMedicines;
        }

    }


    @GetMapping("/updateByIdmedicines")
    public void updateById(Integer smid, Integer count, Double purchase, Double retail, String number) {
        System.out.println("stid是：" + smid + "采购价是" + purchase + "销售价是:" + retail);
        if (smid == null) {
            System.out.println("修改的smid为"+null);
        } else {
            //修改的同时改总价
            StorageMedicine storageMedicine = basicmessageapper.selectSmid(smid);
            basicmessageapper.updeate(count, purchase, retail, smid);
            basicmessageapper.UpdatALLSmid(count*purchase,count*retail,smid);

            System.out.println("storageMedicine.getCount()"+storageMedicine.getCount()+"count"+count);


        //如果改的个数大于之前 传进来的-查的个数

//            Integer counts = basicmessageapper.selectAllcount(storageMedicine.getName());
//            System.out.println("查出来的个数："+counts);
//            Integer thiscounts=counts+count-storageMedicine.getCount()-20;
//            Iasicmessageapper.updatecount(storageMedicine.getName(),thiscounts);
//
//
//            //查出来的总个数加上要加入的采购价
//            Double purchaseamount = basicmessageapper.selectAllpurchases(storageMedicine.getName());
//            System.out.println("查出来的采购价："+purchaseamount);
//            Iasicmessageapper.updatepurchase(storageMedicine.getName(),purchaseamount);
//
//
//            //查出来的总个数加上要加入的销售价
//            Double retailamount = basicmessageapper.selectAllretials(storageMedicine.getName());
//            Iasicmessageapper.updateretail(storageMedicine.getName(),retailamount);
//           System.out.println("查出来的销售价："+retailamount);

        }
    }


    @GetMapping("/selectmedicineAllpurchases")
    public Double selectmedicineAll(Integer stid, String number) {
        System.out.println("stid是：" + stid);
        System.out.println("单号是：" + number);


            Double aDouble = basicmessageapper.selectAllpurchase(number);
        System.out.println("采购s总数是+"+aDouble);
            return aDouble;

    }


    @GetMapping("/selectmedicineAllretails")
    public Double selectmedicineAllretail(Integer stid, String number) {
        System.out.println("stid是：" + stid);
        System.out.println("单号是：" + number);


            Double aDouble = basicmessageapper.selectAllretial(number);
        System.out.println("售价总数是+"+aDouble);
            return aDouble;

    }


//更改订单的采购和销售的总价
    @GetMapping("/updatestoragemedicines")
    public void updatestoragemedicine(Double purchase,Double retail, String number) {
        System.out.println("purchase是：" + purchase);
        System.out.println("retail是：" + retail);
        System.out.println("单号是：" + number);

        masicmessageapper.updtemessage(number,purchase,retail);



    }
}